package com.encryption.logic.impl;

import java.util.List;

import com.encryption.dao.Condition;
import com.encryption.model.User;

public class FileConditionSqlParser {
	
	public static String parse(User user, List<Condition> condList){
		List<Condition> validCondList = condList;
		String condSql = "";
		for (Condition cond : condList){
			if("hasOperate".equalsIgnoreCase(cond.getCode())){
				validCondList.remove(cond);
				String userCondition = "";
				Object userId = null;
				if(user == null){
					userId = findUserId(condList);
				}else{
					userId = user.getId();
				}
				if(userId != null && !"".equals(userId))
					userCondition = "where user_id="+ userId ;
				String join = "";
				if(cond.getValue().equals(false))
					join = "not";
				condSql = "and id " + join + " in (select distinct file_id from file_log " + userCondition + ") ";
				break;
			}
		}
		
		String validCondSql = ConditionSqlParser.parse(validCondList);
		condSql += validCondSql;
		
		return condSql;
	}
	
	public static String parse(List<Condition> condList){
		return parse(null, condList);
	}
	
	private static Object findUserId(List<Condition> condList){
		for (Condition cond : condList){
			if("user_id".equalsIgnoreCase(cond.getCode()))
				return cond.getValue();
		}
		
		return null;
	}

}
